<?php
	
	/**
	 * PkView File
	 * 
	 * @author Brian Bal <brian@youbeqb.com>
	 * @version 1.0
	 * @package pink-koala
	 */
	
	/**
	 * PkView class
	 *
	 * @package pink-koala
	 * @author Brian Bal
	 **/
	
	class PkView
	{
		/**
		 * page function
		 *
		 * @return void
		 * @author Brian Bal
		 */
		
		function page()
		{
			$view_content = $this->view();
			$layout_content = $this->layout($view_content);
			echo $layout_content;
		}
		
		
		
		/**
		 * layout function
		 *
		 * @param string $content 
		 * @return void
		 * @author Brian Bal
		 */
		
		function layout($content)
		{
			$layout_content = "";
			if($this->layout != null)
			{
				$view_file = PROJECT_DIR . "app" . DS . "views" . DS . "layouts" . DS . "$this->layout.html";
				// display layout with content for page in $content
				if(file_exists($view_file))
				{
					ob_start();
					include($view_file);
					$layout_content = ob_get_contents();
					ob_end_clean();
				}else{
					trigger_error("Layout View file not found ($view_file)");
				}
			}
			
			return $layout_content;
		}
		
		
		
		/**
		 * view function
		 *
		 * @return void
		 * @author Brian Bal
		 */
		
		function view()
		{
			$content = "";
			if($this->view_name != null)
			{
				// get the html from view into $content
				$view_file = PROJECT_DIR . "app" . DS . "views" . DS . $this->controller_name . DS . "$this->view_name.html";
				
				if(file_exists($view_file))
				{
					ob_start();
					include($view_file);
			        $content = ob_get_contents();
					ob_end_clean();
				}else{
					trigger_error("View file not found", E_USER_WARNING);
				}
			}
			
			return $content;
		}
		
		
		
		/**
		 * partial function
		 *
		 * @param string $partial_name 
		 * @param string $args 
		 * @return void
		 * @author Brian Bal
		 */
			
		function partial($partial_name, $args = null)
		{
			$partial_filename = PROJECT_DIR . "app/views/partials/{$partial_name}.html";
			
			if($args == null){ $args = array(); }
			
			// make the partial view able to access variables
			foreach($args as $key => $value)
			{
				$$key = $value;
			}
			$args = null;
			
			if(file_exists($partial_filename))
			{
				include($partial_filename);
			}else{
				trigger_error("Partial View file not found");
			}
			
		}
		
		
		
		//
		//	Tag Helper Functions
		//
		
		/**
		 * tag function
		 *
		 * This function will create a xhtml tag for the passed name,
		 * add attributes that are passed in html_options, included
		 * the cdata inside of the tag, and if you want a short tag
		 * set the short tag parameter as true
		 * 
		 * @param string $name 
		 * @param string $html_options 
		 * @param string $cdata 
		 * @param string $short_tag 
		 * @return string
		 * @author Brian Bal
		 */
		
		function tag($name, $html_options = null, $cdata = null, $short_tag = false)
		{
			if($html_options == null){ $html_options = array(); }

			// order the options by key
			ksort($html_options);

			// start the tag
			$html = "<$name";

			// include the html options
			foreach($html_options as $key => $value)
			{
				$key = htmlentities($key);
				$value = htmlentities($value);
				$html = $html . " $key=\"$value\"";
			}

			// close the tag
			if($cdata === null)
			{
				if($short_tag)
				{
					$html = $html . " >";
				}else{
					$html = $html . "/>";
				}

			}else{
				$html = $html . ">$cdata</$name>";
			}

			return $html;
		}
		
		
		
		
		
		function href($options)
		{
			$href = "";
			if(isset($option['url']))
			{
				$href = $options['url'];
				if(! preg_match("/^http/", $href))
				{
					$href = "http://$href";
				}
			}
			else
			{
				if(isset($options['controller']))
				{
					$href = WWW_DIR . $options['controller'];
				}else{
					$href = WWW_DIR . $this->controller_name;
				}
				
				if(isset($options['action']))
				{
					$href .= "/" . $options['action'];
					
					if(isset($options['id']))
					{
						$href .= "/" . $options['id'];
					}
				}
			}
			
			return htmlentities($href);
		}
		
		
		/**
		 * link_to function
		 *
		 * this function will create a anchor tag with a link to the
		 * correct page as passed in the options parameter
		 *
		 * The option parameter should be:
		 * 		controller => controller name
		 * 		action => action name
		 * 		id => any model id
		 *
		 * @param string $name 
		 * @param string $options 
		 * @param string $html_options 
		 * @return string
		 * @author Brian Bal
		 */
		
		function link_to($name, $options = null, $html_options = null)
		{
			if($options == null){ $options = array(); }
			if($html_options == null){ $html_options = array(); }
			
			$html_options['href'] = $this->href($options);
			
			return $this->tag("a", $html_options, $name);
		}
		
		
		
		/**
		 * image_tag function
		 *
		 * @param string $image 
		 * @param string $alt 
		 * @param string $html_options 
		 * @param string $return_html 
		 * @return string
		 * @author Brian Bal
		 */
		
		function image_tag($image, $alt, $html_options = null, $return_html = false)
		{
			if($html_options == null){ $html_options = array(); }

			$img_html = "<img alt=\"$alt\" src=\"" . ROOT_PATH . "images/$image\" ";

			foreach($html_options as $key => $value)
			{
				$img_html = $img_html . "$key=\"$value\" ";
			}

			$img_html = $img_html ."\>";

			if($return_html)
			{
				return $img_html;
			}

			echo $img_html;
		}
		
		
		
		/**
		 * image link to function
		 *
		 * this function will create a anchor tag with a image
		 * tag inside of it
		 *
		 * @param string $image 
		 * @param string $alt 
		 * @param string $image_html_options 
		 * @param string $link_html_options 
		 * @return string
		 * @author Brian Bal
		 */
		
		function image_link_to($image, $alt, $image_html_options = null, $link_html_options = null)
		{
			link_to( image_tag($image, $alt, $image_html_options, true), $link_html_options);
		}
		
		
		
		/**
		 * javascript_tag function
		 *
		 * @param string $script 
		 * @param string $is_file 
		 * @return void
		 * @author Brian Bal
		 */
		
		function javascript_tag($script, $is_file = true)
		{
			$html = "";
			if($is_file)
			{
				$html = "\n\t<script src=\"" . WWW_DIR . "javascript/$script\" type=\"text/javascript\" charset=\"utf-8\"></script>\n";
			}else{
				$html = "\n\t<script type=\"text/javascript\" charset=\"utf-8\">\n";
				$html .= $script;
				$html .= "\n\t</script>\n\n";
			}

			echo $html;
		}
		
		
		
		/**
		 * stylesheet_tag function
		 *
		 * @param string $style 
		 * @param string $media 
		 * @param string $is_file 
		 * @param string $conditional_comment 
		 * @return string
		 * @author Brian Bal
		 */
		
		function stylesheet_tag($style, $media = "screen", $is_file = true, $conditional_comment = false)
		{
			if($is_file)
			{
				$html = "\n\t<link rel=\"stylesheet\" href=\"" . WWW_DIR . "css/$style.css\" type=\"text/css\" media=\"$media\" />\n";
			}else{
				$html = "\n\t<style type=\"text/css\" media=\"$media\">\n";
				$html .= $style;
				$html .= "\n\t</style>\n";
			}

			if($conditional_comment)
			{
				$html = "<!--[if $conditional_comment]>" . $html . "<![endif]-->\n";
			}


			echo $html;
		}
		
		
		
		/**
		 * check_box_tag function
		 *
		 * @param string $name 
		 * @param string $value 
		 * @param string $checked 
		 * @param string $options 
		 * @param string $label 
		 * @return string
		 * @author Brian Bal
		 */
		
		function check_box_tag($name, $value, $checked = false, $options = null, $label = false)
		{
			if($options == null){ $options = array(); }

			// add the type to the options list
			$options['type'] = "checkbox";
			$options['name'] = $name;
			$options['id'] = $name;
			$options['value'] = $value;

			if($options['value'] == 0 && $checked)
			{
				unset($options['id']);
			}


			// add checked to the options list if needed
			if($checked)
			{
				$options['checked'] = "checked";
			}

			// build the tag
			$html = "";
			if($label != false)
			{
				$html = $html . tag("label", array("for" => $name), $label) . " ";
			}
			$html = $html . tag("input", $options, null, true);

			// echo the tag
			echo $html;
		}
		
		
		
		/**
		 * file_field_tag function
		 *
		 * @param string $name 
		 * @param string $options 
		 * @param string $label 
		 * @return string
		 * @author Brian Bal
		 */
		
	    function file_field_tag($name, $options = null, $label = false)
		{
			if($options == null){ $options = array(); }

			// build up the options
			$options['type'] = "file";
			$options['name'] = $name;
			$options['id'] = $name;

			// build the tag
			$html = "";
			if($label)
			{
				$html = $html . tag("label", array("for" => $name), $label) . " ";
			}
			$html = $html . tag("input", $options);

			// echo the tag
			echo $html;
		}
		
		
		
		/**
		 * hidden_field_tag function
		 *
		 * @param string $name 
		 * @param string $value 
		 * @param string $options 
		 * @param string $label 
		 * @return string
		 * @author Brian Bal
		 */
		
	    function hidden_field_tag($name, $value, $options = null, $label = false)
		{
			if($options == null){ $options = array(); }

			// build up the options list
			$options['type'] = "hidden";
			$options['name'] = $name;
			$options['id'] = $name;
			$options['value'] = $value;

			// build the tag
			$html = "";
			if($label)
			{
				$html = $html . tag("label", array("for" => $name), $label) . " ";
			}
			$html = $html . tag("input", $options);

			// echo the tag
			echo $html;
		}
		
		
		
		/**
		 * password_field_tag function
		 *
		 * @param string $name 
		 * @param string $value 
		 * @param string $options 
		 * @param string $label 
		 * @return void
		 * @author Brian Bal
		 */
		
	    function password_field_tag($name, $value, $options = null, $label = false)
		{
			if($options == null){ $options = array(); }

			// build up the options list
			$options['type'] = "password";
			$options['name'] = $name;
			$options['id'] = $name;
			$options['value'] = $value;

			// build the tag
			$html = "";
			if($label)
			{
				$html = $html . $this->tag("label", array("for" => $name), $label) . " ";
			}
			$html = $html . $this->tag("input", $options);

			// echo the tag
			echo $html;
		}
		
		
		
		/**
		 * radio_button_tag function
		 *
		 * @param string $name 
		 * @param string $value 
		 * @param string $checked 
		 * @param string $options 
		 * @param string $label 
		 * @return string
		 * @author Brian Bal
		 */
		
	    function radio_button_tag($name, $value, $checked = false, $options = null, $label = false)
		{
			if($options == null){ $options = array(); }

			// build up the options list
			$options['type'] = "radio";
			$options['name'] = $name;
			$options['id'] = $name;
			$options['value'] = $value;

			// add checked to the options list if needed
			if($checked)
			{
				$options['checked'] = "checked";
			}

			// build the tag
			$html = "";
			if($label)
			{
				$html = $html . tag("label", array("for" => $name), $label) . " ";
			}
			$html = $html . tag("input", $options);

			// echo the tag
			echo $html;
		}
		
		
		
		/**
		 * select_tag function
		 *
		 * @param string $name 
		 * @param string $option_tags 
		 * @param string $value 
		 * @param string $options 
		 * @param string $label 
		 * @return string
		 * @author Brian Bal
		 */
		
	    function select_tag($name, $option_tags = null, $value = null, $options = null, $label = false)
		{
			if($options == null){ $options = array(); }
			if($option_tags == null){ $option_tags = array(); }

			// build up the options list
			$options['name'] = $name;
			$options['id'] = $name;

			// build the options tags
			$option_tags_html = "";
			foreach($option_tags as $key => $val)
			{
				$opts = array();
				if($val == $value)
				{
					$opts["selected"] = "true";
				}
				$opts["value"] = $val;
				$option_tags_html = $option_tags_html . "\n\t" . $this->tag("option", $opts, $key) . "\n";
			}

			// build the tag
			$html = "";
			if($label)
			{
				$html = $html . $this->tag("label", array("for" => $name), $label) . "\n";
			}
			$html = $html . $this->tag("select", $options, $option_tags_html);

			// echo the tag
			echo $html;
		}
		
		
		
		/**
		 * submit_tag function
		 *
		 * @param string $name 
		 * @param string $value 
		 * @param string $options 
		 * @param string $label 
		 * @return string
		 * @author Brian Bal
		 */
		
	    function submit_tag($name, $value, $options = null, $label = false)
		{
			if($options == null){ $options = array(); }

			// build up the options list
			$options['type'] = "submit";
			$options['name'] = $name;
			$options['id'] = $name;
			$options['value'] = $value;

			// build the tag
			$html = $this->tag("input", $options);

			// echo the tag
			$html = "";
			if($label)
			{
				$html = $html . $this->tag("label", array("for" => $name), $label) . " ";
			}
			$html = $html . $this->tag("input", $options, null, true);

			echo $html;
		}
		
		
		
		/**
		 * text_area_tag function
		 *
		 * @param string $name 
		 * @param string $value 
		 * @param string $options 
		 * @param string $label 
		 * @return string
		 * @author Brian Bal
		 */
		
	    function text_area_tag($name, $value, $options = null, $label = false)
		{
			if($options == null){ $options = array(); }

			// build up the options list
			$options['name'] = $name;
			$options['id'] = $name;
			if(! isset($options['rows'])){ $options['rows'] = 10; }
			if(! isset($options['cols'])){ $options['cols'] = 40; }

			// build the tag
			$html = "";
			if($label)
			{
				$html = $html . tag("label", array("for" => $name), $label) . " ";
			}

			if($value == null){ $value = " "; }
			$html = $html . tag("textarea", $options, trim($value));

			// echo the tag
			echo $html;
		}
		
		
		
		/**
		 * text_field_tag function
		 *
		 * @param string $name 
		 * @param string $value 
		 * @param string $options 
		 * @param string $label 
		 * @return void
		 * @author Brian Bal
		 */
		
	    function text_field_tag($name, $value, $options = null, $label = false)
		{
			if($options == null){ $options = array(); }

			// build up the options list
			$options['type'] = "text";
			$options['value'] = $value;
			
			if(! isset($options['name']))
			{
				$options['name'] = $name;
			}
			
			if(! isset($options['id']))
			{
				$options['id'] = $name;
			}
			
			// $options['id'] = preg_replace("/[^A-Za-z0-9:_.-]/", "_", $options['id']);
			// $options['id'] = preg_replace("/(^_|_$)/", "", $options['id']);
			
			// if(isset($_POST[$options['name']]))
			// {
			// 	$options['value'] = $_POST[$options['name']];
			// }
			
			// build the tag
			$html = "";
			if($label)
			{
				$html = $html . $this->tag("label", array("for" => $name), $label) . " ";
			}
			$html = $html . $this->tag("input", $options, null);

			// echo the tag
			echo $html;
		}
		
	}
	
?>